import java.sql.*;

/**
 * @author zhangshuw
 * <p>     2019/8/9
 */
public class JdbcUtil {

    private JdbcUtil(){}

    public static JdbcUtil instance = new JdbcUtil();

    private String url = "jdbc:mysql://";
    private String user;
    private String password;

    public Connection getConnection() throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return DriverManager.getConnection(url, user, password);
    }

    public static void close(PreparedStatement pstat, Connection conn) {
        if (pstat != null) {
            try {
                pstat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet rs, PreparedStatement pstat, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close(pstat, conn);
    }

    public JdbcUtil user(String user){
        this.user = user;
        return this;
    }

    public JdbcUtil password(String password){
        this.password = password;
        return this;
    }

    public JdbcUtil host(String host){
        this.url = url + host;
        return this;
    }

}
